package ${filePackageName};

import ${utilPackage}.BeanUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Map;

/**
 * 分页排序参数
 *
 * @author ${projectAuthor}
 * @version 1.0
 * @date ${nowDate}
 */
@ApiModel(value = "查询条件",description="查询条件")
public class QueryParam<T> {

    /**
     * 对象
     */
    @ApiModelProperty(value = "对象")
    private T obj;

    /**
     * 第几页
     */
    @ApiModelProperty(value = "分页参数 第几页",example = "1")
    private Integer pageIndex = 1;

    /**
     * 每页多少条记录
     */
    @ApiModelProperty(value = "分页参数 每页条数",example = "10")
    private Integer pageSize = Integer.MAX_VALUE;

    /**
     * 排序名称 对应数据库字段名
     */
    @ApiModelProperty(value="排序名,对应数据库字段名")
    private String sortName;

    /**
     * 排序顺序 desc 或asc
     */
    @ApiModelProperty(value="排序顺序 desc或asc")
    private String sortOrder;

    public QueryParam(Wrapper wrapper) {
        if(wrapper!=null){
            this.pageIndex = wrapper.getPageIndex()==null?wrapper.getPageIndex():this.pageIndex;
            this.pageSize = wrapper.getPageSize()==null?wrapper.getPageSize():this.pageSize;
            this.sortName = wrapper.getSortName();
            this.sortOrder = wrapper.getSortOrder();
        }
    }

    public QueryParam(T obj) {
        this.obj = obj;
    }

    public QueryParam(Wrapper wrapper,T obj) {
        this.obj = obj;
        if(wrapper!=null){
            this.pageIndex = wrapper.getPageIndex()!=null?wrapper.getPageIndex():this.pageIndex;
            this.pageSize = wrapper.getPageSize()!=null?wrapper.getPageSize():this.pageSize;
            this.sortName = wrapper.getSortName();
            this.sortOrder = wrapper.getSortOrder();
        }
    }

    public QueryParam(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, T obj) {
        this.obj = obj;
        this.pageIndex = pageIndex!=null?pageIndex:this.pageIndex;
        this.pageSize = pageSize!=null?pageSize:this.pageSize;
        this.sortName = sortName;
        this.sortOrder = sortOrder;
    }

    public QueryParam() {
    }

    public QueryParam(Integer pageIndex, Integer pageSize, String sortName, String sortOrder) {
        this.pageIndex = pageIndex!=null?pageIndex:this.pageIndex;
        this.pageSize = pageSize!=null?pageSize:this.pageSize;
        this.sortName = sortName;
        this.sortOrder = sortOrder;
    }

    public QueryParam(Integer pageIndex, Integer pageSize) {
        this.pageIndex = pageIndex!=null?pageIndex:this.pageIndex;
        this.pageSize = pageSize!=null?pageSize:this.pageSize;
    }

    public QueryParam(String sortName, String sortOrder) {
        this.sortName = sortName;
        this.sortOrder = sortOrder;
    }

    public Map<String,Object> toMap() {
        if(this==null){
            return null;
        }
        Map<String, Object> objMap = BeanUtil.beanToMap(this.obj);
        Map<String, Object> queryMap = BeanUtil.beanToMap(this);
        queryMap.remove("obj");
        queryMap.putAll(objMap);
        return queryMap;
    }

}
